import com.github.pagehelper.PageHelper;
import com.qf.weektest.dao.BookDao;
import com.qf.weektest.entity.Book;
import com.qf.weektest.utils.MybatiesUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class GetBookByPage {
    public static void main(String[] args) {
        PrintWriter printWriter=null;
        BookDao bookDao=null;
        int pageSize=2000;
        List<Book> allBooks=null;
        try {
            printWriter=new PrintWriter(new FileWriter("src/main/resources/books.csv"));
            BookDao bookDao1 = MybatiesUtils.getMapper(BookDao.class);
            Integer totalBook = bookDao1.findAllBookCount();
            int count=0;
            if(totalBook%pageSize==0){
                count=totalBook/pageSize;
            }else{
                count=totalBook/pageSize+1;
            }
            int i=1;
            List<Book> temp=new ArrayList<>();
            while (i <= count){
                PageHelper.startPage(i,pageSize);
                bookDao = MybatiesUtils.getMapper(BookDao.class);
                allBooks= bookDao.findAllBook();
                int size=0;
                for (Book book : allBooks) {
                    Integer id=book.getId();
                    String name=book.getName();
                    String author=book.getAuthor();
                    String publish=book.getPublish();
                    Integer sort = book.getSort();

                    printWriter.println(id+","+name+","+author+","+publish+","+sort);
                    System.out.println(book);
                }
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            printWriter.close();
        }
    }
}
